<?php
class MondanosController extends AppController {

	var $name = 'Mondanos';
	var $helpers = array('Html', 'Form', 'CakePtbr.Formatacao');

        function perfil($slug){
		if (!$slug) {
			$this->Session->setFlash(__('Mondano inválido.', true));
			$this->redirect(array('action'=>'index'));
		}

                $this->set('mondano', $this->Mondano->findBySlug($slug));
        }

        function alterar_senha($id = null){
		if (!$id) {
			$this->Session->setFlash(__('Mondano inválido.', true));
			$this->redirect(array('controller'=>'textos','action'=>'destaques'));
		}
		if (!empty($this->data)) {
                        if(!empty($this->data['Mondano']['senha']) &&
                                ($this->data['Mondano']['senha'] == $this->data['Mondano']['senha_confirma']))
                                $this->data['Mondano']['senha'] = md5($this->data['Mondano']['senha']);
                        if ($this->Mondano->save($this->data)) {
                                //$this->Mondano->saveField('senha', md5($this->data['Mondano']['senha']));
				$this->Session->setFlash(__('Senha alterada com sucesso', true));
				$this->redirect(array('action'=>'edit', 'id'=>$id));
			} else {
				$this->Session->setFlash(__('Senha não pôde ser salva. Por favor, tente novamente.', true));
			}
                }
		if (empty($this->data)) {
			$this->data = $this->Mondano->read(null, $id);
		}
        }

	function index() {
		$this->Mondano->recursive = 0;
		$this->set('mondanos', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Mondano.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('mondano', $this->Mondano->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Mondano->create();
			if ($this->Mondano->save($this->data)) {
				$this->Session->setFlash(__('The Mondano has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Mondano could not be saved. Please, try again.', true));
			}
		}
		$aplicativos = $this->Mondano->Aplicativo->find('list');
		$this->set(compact('aplicativos'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Perfil inválido.', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
                    if ($this->Mondano->save($this->data)) {
				$this->Session->setFlash(__('O perfil do mondano foi salvo', true));
				$this->redirect('/'.$this->data['Mondano']['slug']);
			} else {
				$this->Session->setFlash(__('O perfil do mondano não pôde ser salvo. Por favor, tente novamente.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Mondano->read(null, $id);
		}
		$aplicativos = $this->Mondano->AplicativosUsuario->findAllByUsuarioId($id);
		$this->set(compact('aplicativos'));
	}

	function admin_index() {
		$this->Mondano->recursive = 0;
		$this->set('mondanos', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Mondano.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('mondano', $this->Mondano->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Mondano->create();
			if ($this->Mondano->save($this->data)) {
				$this->Session->setFlash(__('The Mondano has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Mondano could not be saved. Please, try again.', true));
			}
		}
		$aplicativos = $this->Mondano->Aplicativo->find('list');
		$this->set(compact('aplicativos'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Mondano', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Mondano->save($this->data)) {
				$this->Session->setFlash(__('The Mondano has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Mondano could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Mondano->read(null, $id);
		}
		$aplicativos = $this->Mondano->Aplicativo->find('list');
		$this->set(compact('aplicativos'));
	}
}
?>